package edu.uwlax.cs.oayonlinestore.client.utils;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

public class Table extends FlexTable {

	public class AddRow {

		public final HorizontalAlignmentConstant CENTER = HasHorizontalAlignment.ALIGN_CENTER;

		private int column;

		public final HorizontalAlignmentConstant LEFT = HasHorizontalAlignment.ALIGN_LEFT;

		public final HorizontalAlignmentConstant RIGHT = HasHorizontalAlignment.ALIGN_RIGHT;

		private int row;

		public AddRow() {
			row = getRowCount();
			column = 0;
		}

		public AddRow(int itemsAfter) {
			row = insertRow(getRowCount() - itemsAfter);
			column = 0;
		}

		public Cell add(Currency val) {
			return add(val.toString()).setHAlign(RIGHT);
		}

		public Cell add(float val) {
			return add(String.valueOf(val)).setHAlign(RIGHT);
		}

		public Cell add(float val, int precision, String unit) {
			// what is an elegant way of formating floats in GWT as
			// String.format() doesn't exist??
			int f = 10;
			for (int i = 1; i < precision; i++)
				f *= 10;
			val *= f;
			val = (int) val;
			val /= f;
			Cell c = add(val + " " + unit);
			return c.setHAlign(RIGHT);
		}

		public Cell add(float val, String unit) {
			return add(val, 2, unit);
		}

		public Cell add(int val) {
			return add(String.valueOf(val)).setHAlign(RIGHT);
		}

		public Cell add(int val, String unit) {
			return add(val + " " + unit).setHAlign(RIGHT);
		}

		public Cell add(String text) {
			setText(row, column, text);
			return new Cell(row, column++);
		}

		public Cell add(Widget widget) {
			setWidget(row, column, widget);
			return new Cell(row, column++);
		}

		public int addC(String text) {
			setHAlign(HasHorizontalAlignment.ALIGN_CENTER);
			setText(row, column, text);
			return column++;
		}

		public Cell addHtml(String html) {
			setHTML(row, column, html);
			return new Cell(row, column++);
		}

		public int addHtmlC(String html) {
			setHAlign(HasHorizontalAlignment.ALIGN_CENTER);
			setHTML(row, column, html);
			return column++;
		}

		public int addHtmlR(String html) {
			setHAlign(HasHorizontalAlignment.ALIGN_RIGHT);
			setHTML(row, column, html);
			return column++;
		}

		public int addWidgetC(Widget widget) {
			setWidget(row, column, widget);
			setHAlign(HasHorizontalAlignment.ALIGN_CENTER);
			return column++;
		}

		public int addWidgetR(Widget widget) {
			setWidget(row, column, widget);
			setHAlign(HasHorizontalAlignment.ALIGN_RIGHT);
			return column++;
		}

		public int getRow() {
			return row;
		}

		private void setHAlign(HorizontalAlignmentConstant align) {
			getCellFormatter().setHorizontalAlignment(row, column, align);
		}
	}

	public class Cell {
		private int column;

		private int row;

		public Cell(int row, int column) {
			super();
			this.row = row;
			this.column = column;
		}

		public Cell bold() {
			setHTML(row, column, "<b>" + getHTML(row, column) + "</b>");
			return this;
		}

		public Cell setColspan(int val) {
			getFlexCellFormatter().setColSpan(row, column, val);
			return this;
		}

		public Cell setHAlign(HorizontalAlignmentConstant align) {
			getFlexCellFormatter().setHorizontalAlignment(row, column, align);
			return this;
		}

		public Cell setRowspan(int val) {
			getFlexCellFormatter().setRowSpan(row, column, val);
			return this;
		}
	}

	public AddRow addRow() {
		return new AddRow();
	}

	public AddRow addRow(int itemsAfter) {
		return new AddRow(itemsAfter);
	}

	public void truncateToSize(int size) {
		for (int i = getRowCount() - 1; i >= size; i--)
			removeRow(i);
	}

}
